package com.unionpay.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.unionpay.async.AsyncTask;
import com.unionpay.async.AsyncTaskPool;
import com.unionpay.entity.TransactionEntity;
import com.unionpay.mapper.TransactionEntityMapper;

/**
 * transaction表操作
 * 
 * @author liuzy
 * @since 2016年4月29日
 */
public class TblTransaction {
	static TransactionEntityMapper mapper = Mybaties.getMapper(TransactionEntityMapper.class);

	/**
	 * 同步保存交易信息
	 */
	public static void save(String transactionid, String blockHash, String blockNumber, String from, String gas, String gasPrice, String hash, String input, String nonce, String to, String transactionIndex, String value) {
		TransactionEntity record = new TransactionEntity();
		record.setTransactionid(transactionid);
		record.setBlockHash(blockHash);
		record.setBlockNumber(blockNumber);
		record.setFrom(from);
		record.setGas(gas);
		record.setGasPrice(gasPrice);
		record.setHash(hash);
		record.setInput(input);
		record.setNonce(nonce);
		record.setTo(to);
		record.setTransactionIndex(transactionIndex);
		record.setValue(value);
		record.setTime(new Date());
		mapper.insert(record);
	}

	/**
	 * 异步保存交易信息
	 */
	public static void asyncSave(String transactionid) {
		String sql = "insert into transaction(`transactionid`,`blockHash`,`blockNumber`,`from`,`gas`,`gasPrice`,`hash`,`input`,`nonce`,`to`,`transactionIndex`,`value`,`time`) value(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		AsyncTask asyncTask = new AsyncTask(sql, transactionid);
		AsyncTaskPool.add(asyncTask);
	}

	/**
	 * 查询某一天的所有transactionid
	 */
	public static List<String> findByDate(String date) {
		List<String> list = new ArrayList<String>();
		List<TransactionEntity> entitys = mapper.selectByDate(date);
		for (TransactionEntity entity : entitys) {
			list.add(entity.getTransactionid());
		}
		return list;
	}
}
